
//获取应用实例
var app = getApp()
Page({
  //页面触底事件

  data: {
    /**
        * 页面配置
        */
    //处理后的Orders
    dealWithOrders:null,
    //原始Orders
    Orders:[],
    //当前请求页面
    //tablebar高度
    tableBarHeight: 0,
    //请求页数
    pagesOn: 1,
    //垂直导航
    TabCur: 0,
    MainCur: 0,
    VerticalNavTop: 0,
    list: [],
    menuHeight: 0,
    //垂直导航结束
    winWidth: 0,
    winHeight: 0,
    status: 0,
    // tab切换
    currentTab: 0,
    //顶部导航栏
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    //轮播图
    cardCur: 0,
    //加载动画
    loadShow: "",
    //下拉加载动画
    //window高
    //底线
    bottomLineShow: 'false',
    //垂直导航结束
    swiperList: [{
      id: 0,
      type: 'image',
      url: 'https://www.yujiancs.cn/images/bg1.jpg'
    }, {
      id: 1,
      type: 'image',
      url: 'https://www.yujiancs.cn/images/lunbo6.jpg',
    }, {
      id: 2,
      type: 'image',
      url: 'https://www.yujiancs.cn/images/lunbo2.JPG'
    }, {
      id: 3,
      type: 'image',
      url: 'https://www.yujiancs.cn/images/lunbo4.JPG'
    }], //轮播图结束

  },
  onLoad: function () {
    //初始化列表
    var that = this;
    wx.request({
      url: 'https://www.yujiancs.cn/Orders/GetOrder', //仅为示例，并非真实的接口地址
      data: {
        _pageNo: that.data.pagesOn,
        _pageSize: 10,
        state: "0"
      },
      method:"POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.statu == true) {
          var names=null;
          for(var i=0;i<res.data.item.length;i++){
            if(res.data.item[i].ImgName!=" "){
              names= res.data.item[i].ImgName.split(',');
            }else{
              names=null;
            }
            //处理数据
            var dealWithOrder=[
            {Title:res.data.item[i].Title,
              Desc:res.data.item[i].Desc,
              Money:res.data.item[i].Money,
              ImgName:names,
              Address:res.data.item[i].Address,
              Add_Time:res.data.item[i].Add_Time,
              Ordertype_name:res.data.item[i].Ordertype_name,
              Username:res.data.item[i].Username,
              Pic:res.data.item[i].Pic,
              Id:res.data.item[i].Id,
              Sex:res.data.item[i].Sex
              }
              ]
              that.setData({
                loadShow: "false",
                Orders:that.data.Orders.concat(dealWithOrder)
              })
          }
        } else {
          that.setData({
            bottomLineShow: "",
            loadShow: "false"
          })
        }
      }
    })

    //垂直导航初始化数据开始
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
    let list = [{}];
    for (let i = 0; i < 26; i++) {
      list[i] = {};
      list[i].name = String.fromCharCode(65 + i);
      list[i].id = i;
    }
    this.setData({
      list: list,
      listCur: list[0]
    })
    //垂直导航初始化数据结束
    var that = this;
    /**
     * 获取系统信息
     */
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });

      },

    });
    //计算左侧菜单栏的高度
    // 然后逐个取出navbar和header的节点信息
    // 根据文档，先创建一个SelectorQuery对象实例
    let query = wx.createSelectorQuery().in(this);
    // 选择器的语法与jQuery语法相同
    query.select('#header').boundingClientRect();
    query.exec((res) => {
      let headerHeitht = res[0].height;
      that.setData({
        menuHeight: that.data.winHeight - headerHeitht
      })
    });
    //获取顶部导航栏的高度
    //选择id
    query.select('.every').boundingClientRect(function (rect) {
      // console.log(rect.width)
      that.setData({
        height: 22
      })
    }).exec();

  },
  //垂直导航开始
  onReady() {
    wx.hideLoading()
  },
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      MainCur: e.currentTarget.dataset.id,
      VerticalNavTop: (e.currentTarget.dataset.id - 1) * 50
    })
  },
  VerticalMain(e) {
    let that = this;
    let list = this.data.list;
    let tabHeight = 0;
    if (this.data.load) {
      for (let i = 0; i < list.length; i++) {
        let view = wx.createSelectorQuery().select("#main-" + list[i].id);
        view.fields({
          size: true
        }, data => {
          list[i].top = tabHeight;
          tabHeight = tabHeight + data.height;
          list[i].bottom = tabHeight;
        }).exec();
      }
      that.setData({
        load: false,
        list: list
      })
    }
    let scrollTop = e.detail.scrollTop + 20;
    for (let i = 0; i < list.length; i++) {
      if (scrollTop > list[i].top && scrollTop < list[i].bottom) {
        that.setData({
          VerticalNavTop: (list[i].id - 1) * 50,
          TabCur: list[i].id
        })
        return false
      }
    }
  },

  /**
     * 滑动切换tab
     */
  bindChange: function (e) {
    var that = this;
    if (e.detail.source == 'touch') {
      that.setData({ currentTab: e.detail.current });
    }

  },
  /**
   * 点击tab切换
   */
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  /**
   * 上拉加载
   */
  //内容到底部的时候加载数据
  getServerData: function () {
    var that = this;
    this.setData({
      bottomLineShow: "false",
      loadShow: "",
      pagesOn: that.data.pagesOn + 1,
    })
    wx.request({
      url: 'https://www.yujiancs.cn/Orders/GetOrder', //仅为示例，并非真实的接口地址
      data: {
        _pageNo: that.data.pagesOn,
        _pageSize: 10,
        state:"0"
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.statu == true) {
          var names;
          for(var i=0;i<res.data.item.length;i++){
            if(res.data.item[i].ImgName!=" "){
              names= res.data.item[i].ImgName.split(',');
            }else{
              names=false;
            }
            //处理数据
          var dealWithOrder=[
            {Title:res.data.item[i].Title,
              Desc:res.data.item[i].Desc,
              Money:res.data.item[i].Money,
              ImgName:names,
              Address:res.data.item[i].Address,
              Add_Time:res.data.item[i].Add_Time,
              Ordertype_name:res.data.item[i].Ordertype_name,
              Username:res.data.item[i].Username,
              Pic:res.data.item[i].Pic,
              Id:res.data.item[i].Id,
              Sex:res.data.item[i].Sex
              }
              ]
            that.setData({
              loadShow: "false",
              Orders:that.data.Orders.concat(dealWithOrder)
            })


          }
          
        } else {
          that.setData({
            bottomLineShow: "",
            loadShow: "false"
          })
        }
      }
    })
  },
  /**
   * 下拉刷新 */
  getNewDate() {
    wx.showLoading({
      title: '正在刷新...',
      mask: true
    });
    var that = this;
    wx.request({
      url: 'https://www.yujiancs.cn/Orders/GetOrder', //仅为示例，并非真实的接口地址
      data: {
        _pageNo: 1,
        _pageSize: 10,
        state:"0"
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        //加載成功，关闭刷新
        wx.hideLoading({
          complete: (res) => { },
        })
        if (res.data.statu == true) {
          var names;
          that.setData({
            Orders:[]
          })


          for(var i=0;i<res.data.item.length;i++){
            if(res.data.item[i].ImgName!=" "){
              names= res.data.item[i].ImgName.split(',');
            }else{
              names=false;
            }
            //处理数据
          var dealWithOrder=[
            {Title:res.data.item[i].Title,
              Desc:res.data.item[i].Desc,
              Money:res.data.item[i].Money,
              ImgName:names,
              Address:res.data.item[i].Address,
              Add_Time:res.data.item[i].Add_Time,
              Ordertype_name:res.data.item[i].Ordertype_name,
              Username:res.data.item[i].Username,
              Pic:res.data.item[i].Pic,
              Id:res.data.item[i].Id,
              Sex:res.data.item[i].Sex
              }
              ]
            that.setData({
              loadShow: "false",
              Orders:that.data.Orders.concat(dealWithOrder)
            })
          }
        } else {
          that.setData({
            bottomLineShow: "",
            loadShow: "false"
          })
        }
      }
    })
  },
  orderDetail(e) {
    var id=e.currentTarget.dataset.orderid
    //判断是否是跑腿，跑腿可看，
    wx.request({
      url: 'https://www.yujiancs.cn/UserInfo/SelectUser',
      data:{
        wxid:app.globalData.openId
      },  method:"POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        if(res.data[0].AuditState=="已授权"){
          wx.navigateTo({
            url: '/pages/orderDetail/orderDetail?id='+id,
          })
        }else if(res.data[0].AuditState=="待审核"){
          wx.showToast({
            title: '跑腿审核中',
            icon:'none'
          })
        }else{
          wx.showModal({
            title: '四次元口袋屋提示',
            content: '你还不是实名认证，是否立即入驻？',
            success (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/becomeErrands/becomeErrands',
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }
       
      }
    })

  },

})